from django.conf import settings
from rest_framework import status
from rest_framework.permissions import IsAuthenticated

from user.models import User
from rest_framework.views import APIView
from TTPP.utils.fdfs.fdfs_storage import FastDFSStorage
from user.serializers.userinfo_serializer import *
from rest_framework.response import Response


class UserInfoViewSet(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        user = User.objects.get(id=request.user.id)
        ser = UserInfoSerializer(instance=user)
        return Response(ser.data)

    def put(self, request):
        user = User.objects.get(id=request.user.id)
        update_data = request.data
        ser_data = UserInfoSerializer(instance=user, data=update_data, partial=True)
        ser_data.is_valid(raise_exception=True)
        ser_data.save()
        # ser_all_data = UserInfoSerializer(instance=user)
        return Response(status=status.HTTP_200_OK)


class UploadAvtarView(APIView):
    """上传头像接口"""

    def post(self,request):
        ret = FastDFSStorage()._save("",request.data.get('img'))
        ret_url = settings.FDFS_URL + ret
        return Response({'imgurl':ret_url})
